<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>browserevent_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_events_browserevent_test.js.html">browserevent_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.events.BrowserEventTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.events.BrowserEventTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line19"></a>goog.require(&#39;goog.events.BrowserFeature&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line23"></a>
<a name="line24"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line25"></a>var Button = goog.events.BrowserEvent.MouseButton;
<a name="line26"></a>
<a name="line27"></a>function setUp() {
<a name="line28"></a>  stubs.reset();
<a name="line29"></a>}
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * @see https://bugzilla.mozilla.org/show_bug.cgi?id=497780
<a name="line34"></a> */
<a name="line35"></a>function testInvalidNodeBug() {
<a name="line36"></a>  if (!goog.userAgent.GECKO) return;
<a name="line37"></a>
<a name="line38"></a>  var event = {};
<a name="line39"></a>  event.relatedTarget = {};
<a name="line40"></a>  event.relatedTarget.__defineGetter__(
<a name="line41"></a>      &#39;nodeName&#39;,
<a name="line42"></a>      function() {
<a name="line43"></a>        throw Error(&#39;https://bugzilla.mozilla.org/show_bug.cgi?id=497780&#39;);
<a name="line44"></a>      });
<a name="line45"></a>  assertThrows(function() { return event.relatedTarget.nodeName; });
<a name="line46"></a>
<a name="line47"></a>  var bEvent = new goog.events.BrowserEvent(event);
<a name="line48"></a>  assertEquals(event, bEvent.event_);
<a name="line49"></a>  assertNull(bEvent.relatedTarget);
<a name="line50"></a>}
<a name="line51"></a>
<a name="line52"></a>function testPreventDefault() {
<a name="line53"></a>  var event = {};
<a name="line54"></a>  event.defaultPrevented = false;
<a name="line55"></a>  var bEvent = new goog.events.BrowserEvent(event);
<a name="line56"></a>  assertFalse(bEvent.defaultPrevented);
<a name="line57"></a>  bEvent.preventDefault();
<a name="line58"></a>  assertTrue(bEvent.defaultPrevented);
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function testDefaultPrevented() {
<a name="line62"></a>  var event = {};
<a name="line63"></a>  event.defaultPrevented = true;
<a name="line64"></a>  var bEvent = new goog.events.BrowserEvent(event);
<a name="line65"></a>  assertTrue(bEvent.defaultPrevented);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function testIsButtonIe() {
<a name="line69"></a>  stubs.set(goog.events.BrowserFeature, &#39;HAS_W3C_BUTTON&#39;, false);
<a name="line70"></a>  assertIsButton(
<a name="line71"></a>      createBrowserEvent(&#39;mousedown&#39;, 1),
<a name="line72"></a>      Button.LEFT,
<a name="line73"></a>      true);
<a name="line74"></a>  assertIsButton(
<a name="line75"></a>      createBrowserEvent(&#39;click&#39;, 0),
<a name="line76"></a>      Button.LEFT,
<a name="line77"></a>      true);
<a name="line78"></a>  assertIsButton(
<a name="line79"></a>      createBrowserEvent(&#39;mousedown&#39;, 2),
<a name="line80"></a>      Button.RIGHT,
<a name="line81"></a>      false);
<a name="line82"></a>  assertIsButton(
<a name="line83"></a>      createBrowserEvent(&#39;mousedown&#39;, 4),
<a name="line84"></a>      Button.MIDDLE,
<a name="line85"></a>      false);
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function testIsButtonWebkitMac() {
<a name="line89"></a>  stubs.set(goog.events.BrowserFeature, &#39;HAS_W3C_BUTTON&#39;, true);
<a name="line90"></a>  stubs.set(goog.userAgent, &#39;WEBKIT&#39;, true);
<a name="line91"></a>  stubs.set(goog.userAgent, &#39;MAC&#39;, true);
<a name="line92"></a>  assertIsButton(
<a name="line93"></a>      createBrowserEvent(&#39;mousedown&#39;, 0),
<a name="line94"></a>      Button.LEFT,
<a name="line95"></a>      true);
<a name="line96"></a>  assertIsButton(
<a name="line97"></a>      createBrowserEvent(&#39;mousedown&#39;, 0, true),
<a name="line98"></a>      Button.LEFT,
<a name="line99"></a>      false);
<a name="line100"></a>  assertIsButton(
<a name="line101"></a>      createBrowserEvent(&#39;mousedown&#39;, 2),
<a name="line102"></a>      Button.RIGHT,
<a name="line103"></a>      false);
<a name="line104"></a>  assertIsButton(
<a name="line105"></a>      createBrowserEvent(&#39;mousedown&#39;, 2, true),
<a name="line106"></a>      Button.RIGHT,
<a name="line107"></a>      false);
<a name="line108"></a>  assertIsButton(
<a name="line109"></a>      createBrowserEvent(&#39;mousedown&#39;, 1),
<a name="line110"></a>      Button.MIDDLE,
<a name="line111"></a>      false);
<a name="line112"></a>  assertIsButton(
<a name="line113"></a>      createBrowserEvent(&#39;mousedown&#39;, 1, true),
<a name="line114"></a>      Button.MIDDLE,
<a name="line115"></a>      false);
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>function testIsButtonGecko() {
<a name="line119"></a>  stubs.set(goog.events.BrowserFeature, &#39;HAS_W3C_BUTTON&#39;, true);
<a name="line120"></a>  stubs.set(goog.userAgent, &#39;GECKO&#39;, true);
<a name="line121"></a>  stubs.set(goog.userAgent, &#39;MAC&#39;, true);
<a name="line122"></a>  assertIsButton(
<a name="line123"></a>      createBrowserEvent(&#39;mousedown&#39;, 0),
<a name="line124"></a>      Button.LEFT,
<a name="line125"></a>      true);
<a name="line126"></a>  assertIsButton(
<a name="line127"></a>      createBrowserEvent(&#39;mousedown&#39;, 2, true),
<a name="line128"></a>      Button.RIGHT,
<a name="line129"></a>      false);
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function createBrowserEvent(type, button, opt_ctrlKey) {
<a name="line133"></a>  return new goog.events.BrowserEvent({
<a name="line134"></a>    type: type,
<a name="line135"></a>    button: button,
<a name="line136"></a>    ctrlKey: !!opt_ctrlKey
<a name="line137"></a>  });
<a name="line138"></a>}
<a name="line139"></a>
<a name="line140"></a>function assertIsButton(event, button, isActionButton) {
<a name="line141"></a>  for (var key in Button) {
<a name="line142"></a>    assertEquals(
<a name="line143"></a>        &#39;Testing isButton(&#39; + key + &#39;) against &#39; +
<a name="line144"></a>        button + &#39; and type &#39; + event.type,
<a name="line145"></a>        Button[key] == button, event.isButton(Button[key]));
<a name="line146"></a>  }
<a name="line147"></a>
<a name="line148"></a>  assertEquals(isActionButton, event.isMouseActionButton());
<a name="line149"></a>}
</pre>


</body>
</html>
